package com.wlz.learnjuc.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author wlz
 * @date 2020-09-23
 * ReentrantLock
 */
public class ReentrantLockTest {

    private static int sum = 0;

    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {

        for (int i = 0; i < 3; i++) {
            Thread thread = new Thread(() -> {
                lock.lock(); // state=1
                try {
                    for (int j = 0; j < 10000; j++) {
                        sum++;
                        //     lock.lock(); // state=2
                        // todo
                        //    lock.unlock();// state=1
                    }
                } finally {
                    lock.unlock(); // state=0
                }
            });
            thread.start();
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(sum);


    }

}
